<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h1>管理后台登录</h1>
        <p>请输入您的账号和密码</p>
      </div>
      
      <n-form
        ref="formRef"
        :model="formData"
        :rules="rules"
        size="large"
        label-placement="left"
      >
        <n-form-item path="username">
          <n-input
            v-model:value="formData.username"
            placeholder="请输入用户名"
            clearable
            :input-props="{ autocomplete: 'username' }"
          >
            <template #prefix>
              <n-icon>
                <PersonOutline />
              </n-icon>
            </template>
          </n-input>
        </n-form-item>
        
        <n-form-item path="password">
          <n-input
            v-model:value="formData.password"
            type="password"
            placeholder="请输入密码"
            show-password-on="click"
            :input-props="{ autocomplete: 'current-password' }"
            @keyup.enter="handleLogin"
          >
            <template #prefix>
              <n-icon>
                <LockClosedOutline />
              </n-icon>
            </template>
          </n-input>
        </n-form-item>
        
        <n-form-item>
          <n-button
            type="primary"
            size="large"
            block
            :loading="authStore.loading"
            @click="handleLogin"
          >
            {{ authStore.loading ? '登录中...' : '登录' }}
          </n-button>
        </n-form-item>
      </n-form>
      
      <div class="login-footer">
        <p>系统管理员登录</p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
import {
  NForm,
  NFormItem,
  NInput,
  NButton,
  NIcon,
  useMessage
} from 'naive-ui'
import { PersonOutline, LockClosedOutline } from '@vicons/ionicons5'
import { useAuthStore } from '@/store/auth'

const router = useRouter()
const { t } = useI18n({ useScope: 'global' })
const message = useMessage()
const authStore = useAuthStore()

const formRef = ref()

// 表单数据
const formData = reactive({
  username: '',
  password: ''
})

// 表单验证规则
const rules = {
  username: {
    required: true,
    message: '请输入用户名',
    trigger: 'blur'
  },
  password: {
    required: true,
    message: '请输入密码',
    trigger: 'blur'
  }
}

// 处理登录
const handleLogin = async () => {
  try {
    // 表单验证
    await formRef.value?.validate()
    
    // 调用store的登录方法
    const result = await authStore.login(formData.username, formData.password)
    console.log(result)
    if (result.success) {
      message.success(result.message)
      // 跳转到首页
      router.push('/')
    } else {
      message.error(result.message)
    }
  } catch (error: any) {
    console.error('登录错误:', error)
    message.error('登录过程中发生错误')
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-box {
  background: white;
  padding: 40px;
  border-radius: 12px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
  position: relative;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-header h1 {
  color: #333;
  margin: 0 0 8px 0;
  font-size: 28px;
  font-weight: 600;
}

.login-header p {
  color: #666;
  margin: 0;
  font-size: 14px;
}

.login-footer {
  text-align: center;
  margin-top: 30px;
}

.login-footer p {
  color: #999;
  margin: 0;
  font-size: 12px;
}

:deep(.n-form-item) {
  margin-bottom: 24px;
}

:deep(.n-form-item:last-child) {
  margin-bottom: 0;
}

:deep(.n-input) {
  height: 48px;
}

:deep(.n-button) {
  height: 48px;
  font-size: 16px;
  font-weight: 500;
}

:deep(.n-input__input-el) {
  font-size: 14px;
}

:deep(.n-input__placeholder) {
  color: #bbb;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-container {
    padding: 10px;
  }
  
  .login-box {
    padding: 30px 20px;
  }
  
  .login-header h1 {
    font-size: 24px;
  }
}
</style>